package org.roger.search;

import java.io.IOException;

import org.apache.lucene.index.Term;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.spans.SpanFirstQuery;
import org.apache.lucene.search.spans.SpanTermQuery;

/**
 * 跨度查询
 * @param field 所搜的字段 
 * @param phrase 匹配短语
 * @param firstIndex 域的开始几个范围内跨度
 * @author luoweiyi
 */
public class SpanFirstSearch extends LuceneSearchBase{
	
	private String field;
	private String phrase;
	private int firstIndex;
	
	public SpanFirstSearch(int firstIndex,String field,String phrase){
		this.firstIndex = firstIndex;
		this.field = field;
		this.phrase = phrase;
	}

	@Override
	public Query getSearchQuery() {
		SpanTermQuery termquery = new SpanTermQuery(new Term(field,phrase));
		
		//域的起点几个位置跨度查找
		SpanFirstQuery firstQuery = new SpanFirstQuery(termquery,firstIndex);
		return firstQuery;
	}
	
	public static void main(String[] args) {
		SpanFirstSearch search = new SpanFirstSearch(4,"contents","freshness");
		try {
			search.search("D:/index");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public Filter getFilter() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Sort getSort() {
		// TODO Auto-generated method stub
		return null;
	}
}
